package com.luocj.elephant.demo.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.luocj.elephant.demo.Bean.LineChartBean;
import com.luocj.elephant.demo.R;
import com.luocj.elephant.demo.view.MyCurveChartView;

import java.util.Random;


public class LineChartActivity extends AppCompatActivity {


    private String TAG = LineChartActivity.class.getSimpleName();
    private static final int RANDOM = 1001;
    private TextView value;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            switch (what) {
                case RANDOM:
                    Random random = new Random();
                    LineChartBean lineChartBean = new LineChartBean();
                    lineChartBean.setyAxis(random.nextFloat() * 300);
                    myView.updataView(lineChartBean);
                    Message message = new Message();
                    message.what = RANDOM;
                    mHandler.sendMessageDelayed(message, 2000);
                    break;
            }

            super.handleMessage(msg);

        }
    };
    private MyCurveChartView myView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_chart);
        myView = findViewById(R.id.myview);
        value = findViewById(R.id.tv_value);

        myView.setOnTouchListener(new MyCurveChartView.OnPointTouch() {
            @Override
            public void OnPointTouch(int clickIndex) {
                Log.i(TAG, "OnPointTouch: " + clickIndex);
                value.setText(clickIndex + "");
            }
        });

        findViewById(R.id.btn_add_point).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message message = new Message();
                message.what = RANDOM;
                mHandler.sendMessageDelayed(message, 2000);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeMessages(RANDOM);
    }
}
